CC = $(CROSS)gcc
VIXH = -I./Libraries/ -I/usr/include/vmware-vix -lvixAllProducts -ldl -lncurses

CFILES = main.c validation.c Core/connect.c Core/list_vm.c Core/vm_open.c Core/vm_info.c Core/vm_power.c Core/ui_help.c Core/VMtools.c

COMMON_CFLAGS =  -Werror -Wall -Wextra -pedantic -pedantic-errors \
	  -Wformat-security -Wformat-nonliteral -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings \
	  -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls \
	  -Wnested-externs -Wno-long-long \
	  -Wstrict-prototypes -Wno-unused -std=gnu99
X86_CFLAGS = -m32 -O3 $(VIXH) $(COMMON_FLAGS)
X64_CFLAGS = -m64 -O3 -msse2 -mfpmath=sse $(VIXH) $(COMMON_FLAGS)


ARCH ?= $(shell uname -m | sed -e s/i.86/x86/ -e s/x86_64/x64/)

## Set variables as appropriate for the arch we're compiling for.
ifeq ($(ARCH),x86)
CFLAGS = $(X86_CFLAGS)
endif
ifeq ($(ARCH),x64)
CFLAGS = $(X64_CFLAGS)
endif

## The file that indicates the directories have been created
MKDIRS = build/.mkdirs.done
DIRS = build/ build/Core/
DEPFILES = $(addprefix build/,$(CFILES:.c=.d))
ifneq ($(MAKECMDGOALS),clean)
-include $(DEPFILES)
endif

$(DEPFILES): build/%.d: %.c $(MKDIRS)
	./depend.sh build/$(dir $<) $(CFLAGS) $*.c > $@
$(MKDIRS):
	for i in $(DIRS); do [ -d $$i ] || mkdir -v $$i; done
	touch $@

# link the object files together
build/vicl-$(ARCH): $(MKDIRS) $(DEPFILES) $(addprefix build/,$(CFILES:.c=.o))
	$(CC) $(VIXH) -o $@ $(addprefix build/, $(CFILES:.c=.o))

# compile the C source files
$(addprefix build/,$(CFILES:.c=.o)): build/%.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $<


clean:
	$(RM) -r build/

all: $(DEPFILES) build/vicl-$(ARCH)

